﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
//svnkit imports
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
    class SVNResolved : CommandBase
    {
        // Initialise variables with default settings.
        public java.io.File filePath; //path of file to be marked resolved
        SVNDepth depth = SVNDepth.INFINITY;
        SVNConflictChoice choice = SVNConflictChoice.POSTPONE;
        //Available choices - BASE, MERGED, MINE_CONFLICT, MINE_FULL, THEIRS_CONFLICT, THEIRS_FULL

        // Minimal Constructor
        public SVNResolved(string path)
        {
            Debug.WriteLine("SVNResolved Minimal Constructor.");
            setupResolved(path);
        }

        // Full Constructor
        public SVNResolved(string path, SVNDepth depth, SVNConflictChoice choice)
        {
            Debug.WriteLine("SVNResolved Full Constructor.");
            this.filePath = new java.io.File(path);
            this.depth = depth;
            this.choice = choice;
        }

        private void setupResolved(string path)
        {
            this.filePath = new java.io.File(path);
        }

        public override void Execute()
        {
            Debug.WriteLine("SVNResolved Execution");

            //library setup
            Tools.setupLibrary();

            //load up the clients
            SVNClientManager cm = SVNClientManager.newInstance();
            SVNWCClient wc = cm.getWCClient();

            try
            {
                wc.doResolve(filePath, depth, choice);
            }
            catch (SVNException e)
            {
                throw new SVNExecuteException(e.getMessage());
            }
        }

        public override Dictionary<string, List<object>> getTokens ()
		{
			Dictionary<string, List<object>> ret = new Dictionary<string, List<object>> ();
			ret.Add("Resolved",new List<object>() {"rs","resolved"});
			return ret;
		}
		
		public override List<KeyValuePair<string,initPointer>> getInitPointers() {
			List<KeyValuePair<string,initPointer>> ret = new List<KeyValuePair<string,initPointer>>();
			ret.Add(new KeyValuePair<string,initPointer>("resolved",new initPointer(this.handleCLI)));
			return ret;
		}
		
		public void handleCLI(Dictionary<string,List<object>> tokens) {
			if (tokens["resolved"].Count == 0) {
				Console.WriteLine("Not enough arguments: Resolved requires 1 argument - WC filepath");
				return;
			}
            string fpath = (string)tokens["resolved"][0];
            this.setupResolved(fpath);
            this.Execute();
        }

        public SVNResolved() {}
    }
}
